<?php echo $this->passedArgs['0']; ?>

<div id="TournamentForm" style="display:normal">
	<?php echo $this->Form->create('Tournament', array('controller'=>'Tournaments', 'action'=>'save')); ?>
		<fieldset>
			<?php echo $this->Form->input('court_id', array('empty'=>true,'options' => $courts, 'label'=>'Kort')); ?>
			<?php echo $this->Form->label('Data rezerwacji'); ?>
			<p><input class="datepicker" id="TournamentDatePicker" type="text" style="width:256px"></p>
			<?php echo $this->Form->input('start_time', array('label' => 'Godzina rozpoczęcia', 'empty'=>true, 'options' => array()));?>
			<?php echo $this->Form->input('end_time', array('label' => 'Godzina zakończenia', 'empty'=>true, 'options' => array()));?>
			<?php echo $this->Form->hidden('tournament_name', array('value' => $this->passedArgs['0'])); ?>
			<?php echo $this->Form->end('Dodaj'); ?>
			<?php echo $this->Form->postLink('Zapisz', array('controller'=>'Tournaments', 'action' => 'index')); ?>
			<?php echo $this->Form->postLink('Usuń', array('controller'=>'Tournaments', 'action' => 'delete', $this->passedArgs['0']), array('confirm'=>'Czy napewno chcesz usunąć ten turniej?')); ?>
		</fieldset>
</div>

<table>
	<tr>
		<th><?php echo $this->Paginator->sort('Court.name', 'Kort'); ?></th>
		<th><?php echo $this->Paginator->sort('start_time', 'Data rezerwacji'); ?></th>
	</tr>

	<?php foreach($reservations as $reservation): ?>
	<tr>
		<td><?php echo $reservation['Court']['name']; ?> </td>
		<td><?php echo $reservation['Reservation']['start_time']; ?> </td>
	</tr>
	<?php endforeach; ?>
</table>
<?php echo $this->Paginator->numbers(array('first'=>3,'last' => 3)); ?>

<script>
	$(function() {
		$( ".datepicker" ).datepicker({
			minDate: 0
		});
	});
	jQuery(function($) {
	$.datepicker.regional['pl'] = {
		closeText: 'Zamknij',
		prevText: '&#x3c;Poprzedni',
		nextText: 'Następny&#x3e;',
		currentText: 'Dziś',
		monthNames: ['Styczeń','Luty','Marzec','Kwiecień','Maj','Czerwiec',
		'Lipiec','Sierpień','Wrzesień','Październik','Listopad','Grudzień'],
		monthNamesShort: ['Sty','Lu','Mar','Kw','Maj','Cze',
		'Lip','Sie','Wrz','Pa','Lis','Gru'],
		dayNames: ['Niedziela','Poniedziałek','Wtorek',' Środa','Czwartek','Piątek','Sobota'],
		dayNamesShort: ['Nie','Pn','Wt','Śr','Czw','Pt','So'],
		dayNamesMin: ['N','Pn','Wt','Śr','Cz','Pt','So'],
		weekHeader: 'Tydz',
		dateFormat: 'yy-mm-dd',
		firstDay: 1,
		isRTL: false,
		showMonthAfterYear: false,
		yearSuffix: ''};
	$.datepicker.setDefaults($.datepicker.regional['pl']);
});
	
	// Rezerwacje turniejowe
	$('#TournamentCourtId').change(function() {
		var court_id = "";
		court_id = $('#TournamentCourtId option:selected').val();
		
		$.post('/korty_tenisowe/reservations/jsSetAvailableHours', {dateText:$(this).val(),court_id: court_id}, function(response) {
			
			var obj = jQuery.parseJSON(response);
			var options = $("#TournamentStartTime");
			$("#TournamentStartTime").find('option').remove().end();
			$("#TournamentEndTime").find('option').remove().end();
			
			$('#TournamentDatePicker').val("");
		});
	});
	$('#TournamentDatePicker').change(function() {
		court_id = $('#TournamentCourtId option:selected').val();
		
		$.post('/korty_tenisowe/reservations/jsSetAvailableHours', {dateText:$(this).val(), court_id: court_id}, function(response) {
			
			var obj = jQuery.parseJSON(response);
			var optionsStart = $("#TournamentStartTime");
			var optionsEnd = $("#TournamentEndTime");
			optionsStart.find('option').remove().end();
			optionsEnd.find('option').remove().end();
			
			var first = 1;
			$.each(obj, function(key, value) {
				optionsStart.append($("<option />").val(value).text(key));
				if(first == 1) {
					first = 0;
				} else {
					optionsEnd.append($("<option />").val(value).text(key));
				}
			});
		});
	});
	$("#TournamentStartTime").change(function() {
		var options = $("#TournamentEndTime");
		options.find('option').remove().end();
		$(this).find('option').each(function() {
			if($("#TournamentStartTime").val() < $(this).val()) {
				options.append($(this).clone());
			}
		});
	});
</script>
